﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using System.Data;
using System.Collections;
using EntitySpaces.Interfaces;

public partial class Admin_SAPFin_SAPFin004 : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
            if (Session["Dsd"] != null)
            {
                txtDocumentStartDate.Value = Session["Dsd"].ToString();
            }
            if (Session["Ded"] != null)
            {
                txtDocumentEndDate.Value = Session["Ded"].ToString();
            }
            //if (Session["Ssd"] != null)
            //{
            //    txtStoreStartDate.Value = Session["Ssd"].ToString();
            //}
            //if (Session["Sed"] != null)
            //{
            //    txtStoreEndDate.Value = Session["Sed"].ToString();
            //}
            if (Session["txtReversedocument"] != null) {
                txtReversedocument.Text = Session["txtReversedocument"].ToString();
            }
            if (Session["txtBoxNo"] != null)
            { txtBoxNo.Text = Session["txtBoxNo"].ToString(); }
            //if (Session["txtSupplierBoxNo"] != null)
            //{ txtSupplierBoxNo.Text = Session["txtSupplierBoxNo"].ToString(); }
            //if (Session["drpDocType"] != null)
            //{
            //    drpDocType.SelectedValue = Session["drpDocType"].ToString();
            //}
            //if (Session["txtKey"] != null)
            //{ txtKey.Text = Session["txtKey"].ToString(); }
            if (Session["drpCompany"] != null)
            {
                drpCompany.SelectedValue = Session["drpCompany"].ToString();
            }
            if (Session["drpFunctionDept"] != null)
            {
                drpFunctionDept.SelectedValue = Session["drpFunctionDept"].ToString();
            }
            if (Session["txtUserName"] != null)
            { txtUserName.Text = Session["txtUserName"].ToString(); }
            if (Session["txtDocumentsType"] != null)
            { txtDocumentsType.Text = Session["txtDocumentsType"].ToString(); }
            if (Session["txtReferenceTransact"] != null)
            { txtReferenceTransact.Text = Session["txtReferenceTransact"].ToString(); }
            if (Session["txtDocumentSystem"] != null)
            { txtDocumentSystem.Text = Session["txtDocumentSystem"].ToString(); }
            if (Session["drpStoreStatus"] != null) {
                drpStoreStatus.SelectedValue = Session["drpStoreStatus"].ToString();
            }
            if (Session["txtLogicalSystem"] != null)
            { txtLogicalSystem.Text = Session["txtLogicalSystem"].ToString(); }
            if (Session["drpIsDeleted"] != null)
            {
                drpIsDeleted.SelectedValue = Session["drpIsDeleted"].ToString();
            }
            if (Session["drpIsUpdate"] != null)
            {
                drpIsUpdate.SelectedValue = Session["drpIsUpdate"].ToString();
            }
            if (Session["txtKey"] != null)
            {
                txtKey.Text = Session["txtKey"].ToString();
            }
            if (Session["txtKeyStart"] != null)
            {
                txtKey.Text = Session["txtKeyStart"].ToString();
            }
            if (Session["txtKeyEnd"] != null)
            {
                txtKeyEnd.Text = Session["txtKeyEnd"].ToString();
            }
            if (Session["drpKeyType"] != null)
            {
                drpKeyType.SelectedValue = Session["drpKeyType"].ToString();
            }
            if (Session["txtKeyMulti"] != null)
            { txtKeyMulti.Text = Session["txtKeyMulti"].ToString(); }
        }
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["txtKey"] = txtKey.Text.Trim();
        Session["txtKeyStart"] = txtKeyStart.Text.Trim();
        Session["txtKeyEnd"] = txtKeyEnd.Text.Trim();
        Session["drpKeyType"] = drpKeyType.SelectedValue;
        Session["txtKeyMulti"] = txtKeyMulti.Text.Trim();   
        Session["txtBoxNo"] = txtBoxNo.Text.Trim();
        //Session["txtSupplierBoxNo"] = txtSupplierBoxNo.Text.Trim();
        //Session["drpDocType"] = drpDocType.SelectedValue;
        //Session["txtKey"] = txtKey.Text.Trim();
        Session["drpCompany"] = drpCompany.SelectedValue;
        Session["drpFunctionDept"] = drpFunctionDept.SelectedValue;
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
        Session["Dsd"] = txtDocumentStartDate.Value;
        Session["Ded"] = txtDocumentEndDate.Value;
        //Session["DocuDate"] = txtDocumentDate.Value;
        //Session["Ssd"] = txtStoreStartDate.Value;
        //Session["Sed"] = txtStoreEndDate.Value;
        Session["drpStoreStatus"] = drpStoreStatus.SelectedValue;
        Session["txtUserName"] = txtUserName.Text.Trim();
        Session["txtDocumentsType"] = txtDocumentsType.Text.Trim();
        Session["txtReferenceTransact"] = txtReferenceTransact.Text.Trim();
        Session["txtDocumentSystem"] = txtDocumentSystem.Text.Trim();
        Session["txtLogicalSystem"] = txtLogicalSystem.Text.Trim();
        Session["txtReversedocument"] = txtReversedocument.Text.Trim();
        Session["drpIsDeleted"] = drpIsDeleted.SelectedValue;
        Session["drpIsUpdate"] = drpIsUpdate.SelectedValue;

    }
    protected void btnAdd_Click(object sender, EventArgs e) {    
        if (!string.IsNullOrEmpty(HiddenFieldIds.Value)) { 
            string[] idList = HiddenFieldIds.Value.Split(',');
            using (esTransactionScope tr = new esTransactionScope())
            {
                DateTime now = DateTime.Now;
                string currentUser = LoginUserInfo.CurrentUser.UserName;
                FinReceiptCollection finreceiptcollection = new FinReceiptCollection();
                ArrayList arg = new ArrayList();
                ArrayList argor = new ArrayList();
                for (int i = 0; i < idList.Length; i++) {
                    argor.Add(finreceiptcollection.Query.ReceiptID == idList[i]);                   
                }
                arg.Add(finreceiptcollection.Query.BoxID > 0);
                arg.Add(finreceiptcollection.Query.Or(argor.ToArray()));
                finreceiptcollection.Query.Where(arg.ToArray());
                finreceiptcollection.Query.Load();
                if (finreceiptcollection.Count > 0)
                { //提示出错
                    ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showNews('存在已在盒子里的凭证！')", true);
                }
                else
                {
                    finreceiptcollection = new FinReceiptCollection();
                    finreceiptcollection.Query.Where(finreceiptcollection.Query.Or(argor.ToArray()));
                    finreceiptcollection.Query.Load();
                    foreach(FinReceipt temp in finreceiptcollection)
                    {  
                        temp.IsDeleted = true;
                    }
                    finreceiptcollection.Save();
                    tr.Complete();

                    ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showNews('已经成功删除凭证！');", true);
                }            
            }
        }
    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        FinReceiptQuery q1 = new FinReceiptQuery("q1");
        TSCodeTableQuery q2 = new TSCodeTableQuery("q2");
        TSCodeTableQuery q3 = new TSCodeTableQuery("q3");
        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        TSCodeTableQuery q6 = new TSCodeTableQuery("q6");
        BFThirdUnitQuery q5 = new BFThirdUnitQuery("q5");
        BFCompanyDicQuery q7 = new BFCompanyDicQuery("q7");
        VReceiptExtInfoQuery q8 = new VReceiptExtInfoQuery("q8");
        TSUsersQuery q11 = new TSUsersQuery("q11");
        TSCodeTableQuery q9 = new TSCodeTableQuery("q9");

        q1.LeftJoin(q9).On(q1.VoucherDescription == q9.Code);
        q1.LeftJoin(q2).On(q1.FunctionDept == q2.Code);
        q1.LeftJoin(q3).On(q1.StorageStatus == q3.Code);
        q1.LeftJoin(q4).On(q1.VoucherDescription == q4.Code);
        q1.LeftJoin(q5).On(q1.StorageLocation == q5.ThirdUnitID); 
        q1.LeftJoin(q6).On(q1.Division == q6.Code);
        q1.LeftJoin(q7).On(q1.CompanyCode == q7.CompanyCode);
        q1.LeftJoin(q8).On(q1.ReceiptID == q8.ReceiptID);
        q1.LeftJoin(q11).On(q5.ManageUserId == q11.UserID);
        q1.SelectAll().Select(q11.LoginName,q2.Value1.As("FunctionDeptStr"), q3.Value1.As("StorageStatusStr"), q4.Value1.As("VoucherDescriptionName"), q5.Name.As("StorageLocationStr"), q6.Value1.As("DivisionStr"), q7.CompanyName, q7.CompanyCode, q8.SupplierBoxNo, q8.BoxNo, q9.Value1.As("DocDesc"));

        ArrayList args = new ArrayList();
        ArrayList argsOr = new ArrayList();
        args.Add(q1.IsSAP==true);
        if (!string.IsNullOrEmpty(txtKey.Text.Trim()))
        {
            argsOr.Add(q1.DocumentsNo.Like("%" + txtKey.Text.Trim() + "%"));
            argsOr.Add(q1.Reference.Like("%" + txtKey.Text.Trim() + "%"));
            argsOr.Add(q1.ReferenceKey.Like("%" + txtKey.Text.Trim() + "%"));
            argsOr.Add(q1.DocumentHeaderText.Like("%" + txtKey.Text.Trim() + "%"));
            args.Add(q1.Or(argsOr.ToArray()));
        }
        if (!string.IsNullOrEmpty(txtKeyStart.Text.Trim()))
        {
            argsOr.Add(q1.DocumentsNo >= txtKeyStart.Text.Trim());
            argsOr.Add(q1.Reference >= txtKeyStart.Text.Trim());
            argsOr.Add(q1.ReferenceKey >= txtKeyStart.Text.Trim());
            argsOr.Add(q1.DocumentHeaderText >= txtKeyStart.Text.Trim());
            args.Add(q1.Or(argsOr.ToArray()));
        }

        if (!string.IsNullOrEmpty(txtKeyEnd.Text.Trim()))
        {
            argsOr.Add(q1.DocumentsNo <= txtKeyEnd.Text.Trim());
            argsOr.Add(q1.Reference <= txtKeyEnd.Text.Trim());
            argsOr.Add(q1.ReferenceKey <= txtKeyEnd.Text.Trim());
            argsOr.Add(q1.DocumentHeaderText <= txtKeyEnd.Text.Trim());
            args.Add(q1.Or(argsOr.ToArray()));
        }

        if (!string.IsNullOrEmpty(txtKeyMulti.Text.Trim()))
        {

            switch (drpKeyType.SelectedValue)
            {
                case "1":
                    string[] keys = txtKeyMulti.Text.Trim().Split(new char[] { '\r', '\n' });
                    for (int i = 0; i < keys.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(keys[i]))
                        { argsOr.Add(q1.DocumentsNo == keys[i]); }
                    }
                    break;
                case "2":
                    keys = txtKeyMulti.Text.Trim().Split(new char[] { '\r', '\n' });
                    for (int i = 0; i < keys.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(keys[i]))
                        { argsOr.Add(q1.Reference == keys[i]); }
                    }
                    break;
                case "3":
                    keys = txtKeyMulti.Text.Trim().Split(new char[] { '\r', '\n' });
                    for (int i = 0; i < keys.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(keys[i]))
                        { argsOr.Add(q1.ReferenceKey == keys[i]); }
                    }
                    break;
                case "4":
                    keys = txtKeyMulti.Text.Trim().Split(new char[] { '\r', '\n' });
                    for (int i = 0; i < keys.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(keys[i]))
                        { argsOr.Add(q1.DocumentHeaderText == keys[i]); }
                    }
                    break;
            }
            args.Add(q1.Or(argsOr.ToArray()));
        }
        if (!string.IsNullOrEmpty(drpIsDeleted.SelectedValue))
        {
            args.Add(q1.IsDeleted == (drpIsDeleted.SelectedValue == "1"));
        }
        if (!string.IsNullOrEmpty(drpIsUpdate.SelectedValue))
        {
            args.Add(q1.UpdateCount == drpIsUpdate.SelectedValue);
        }    
        //测试一下
        //if (!string.IsNullOrEmpty(drpDocType.SelectedValue))
        //{
        //    args.Add(q1.VoucherDescription == drpDocType.SelectedValue);
        //}
        //if (!string.IsNullOrEmpty(txtKey.Text.Trim()))
        //{
        //    argsOr.Add(q1.DocumentsNo.Like("%" + txtKey.Text.Trim() + "%"));
        //    argsOr.Add(q1.Reference.Like("%" + txtKey.Text.Trim() + "%"));
        //    argsOr.Add(q1.ReferenceKey.Like("%" + txtKey.Text.Trim() + "%"));
        //    argsOr.Add(q1.DocumentHeaderText.Like("%" + txtKey.Text.Trim() + "%"));
        //    args.Add(q1.Or(argsOr.ToArray()));
        //}
        if (!string.IsNullOrEmpty(drpCompany.SelectedValue))
        {
            args.Add(q1.CompanyCode == drpCompany.SelectedValue.Replace(")","").Split('(')[1]);
            args.Add(q1.CompanyName == drpCompany.SelectedValue.Replace(")", "").Split('(')[0]);
        }
        if (!string.IsNullOrEmpty(drpFunctionDept.SelectedValue))
        {
            args.Add(q1.FunctionDept == drpFunctionDept.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpStoreStatus.SelectedValue)) {
            args.Add(q1.StorageStatus == drpStoreStatus.SelectedValue);
        }
       
        if (!string.IsNullOrEmpty(txtReversedocument.Text.Trim())) {
            args.Add(q1.RevesedBy.Like("%" + txtReversedocument.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtDocumentStartDate.Value))
        {
            args.Add(q1.VoucherDate >= txtDocumentStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtDocumentEndDate.Value))
        {
            args.Add(q1.VoucherDate <= DateTime.Parse(txtDocumentEndDate.Value).AddDays(1));
        }
        //if (!string.IsNullOrEmpty(txtStoreStartDate.Value))
        //{
        //    args.Add(q1.FirstStorageDate >= txtStoreStartDate.Value);
        //}
        //if (!string.IsNullOrEmpty(txtStoreEndDate.Value))
        //{
        //    args.Add(q1.FirstStorageDate <= DateTime.Parse(txtStoreEndDate.Value).AddDays(1));
        //}
        if (!string.IsNullOrEmpty(txtStartDate.Value))
        {
            args.Add(q1.CreatedDttm >= txtStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtEndate.Value))
        {
            args.Add(q1.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        }
        if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
        {
            args.Add(q1.UserName.Like("%" + txtUserName.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtDocumentsType.Text.Trim()))
        {
            args.Add(q1.DocumentsType.Like("%" + txtDocumentsType.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtReferenceTransact.Text.Trim()))
        {
            args.Add(q1.ReferenceTransact.Like("%" + txtReferenceTransact.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtDocumentSystem.Text.Trim()))
        {
            args.Add(q1.DocumentSystem.Like("%" + txtDocumentSystem.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtLogicalSystem.Text.Trim()))
        {
            args.Add(q1.LogicalSystem.Like("%" + txtLogicalSystem.Text.Trim() + "%"));
        }     
        q1.Where(args.ToArray()).OrderBy(q1.CreatedDttm.Ascending);
        q1.es.Top = 1000;
        DataTable tableSorce = q1.LoadDataTable();
        pageGrid.DataSource = tableSorce;
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("drpDocType");
        Session.Remove("txtKey");
        Session.Remove("drpCompany");
        Session.Remove("drpFunctionDept");
        Session.Remove("sd");
        Session.Remove("ed");
        Session.Remove("Dsd");
        Session.Remove("Ded");
        Session.Remove("Ssd");
        Session.Remove("Sed");
        Session.Remove("txtUserName");
        Session.Remove("txtDocumentsType");
        Session.Remove("txtReferenceTransact");
        Session.Remove("txtDocumentSystem");
        Session.Remove("txtLogicalSystem");
        Session.Remove("txtBoxNo");
        Session.Remove("txtSupplierBoxNo");
        Session.Remove("drpStoreStatus");
        Session.Remove("txtReversedocument");
        Session.Remove("drpKeyType");
        Session.Remove("txtKeyMulti");
        Session.Remove("txtKey");
        Session.Remove("txtKeyStart");
        Session.Remove("txtKeyEnd");
    }

    #region  Delete_Command
    protected void Delete_Command(object sender, CommandEventArgs e)
    {
        decimal Id = decimal.Parse(e.CommandArgument.ToString());
        FinReceipt finReceipt = new FinReceipt();
        finReceipt.LoadByPrimaryKey(Id);
        finReceipt.IsDeleted = true;
        finReceipt.Save();
        ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showNews('此凭证已经删除！')", true);


        //FinReceiptCollection finReceiptCollection = new FinReceiptCollection();
        //finReceiptCollection.Query.Where(finReceiptCollection.Query.UploadId == Id, finReceiptCollection.Query.StorageStatus != "S1");
        //finReceiptCollection.Query.Load();
        //if (finReceiptCollection.Count > 0)
        //{
        //    ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showMsg('此Excel上传的凭证有" + finReceiptCollection.Count.ToString() + "份已经入库，不能批量删除！')", true);
        //}
        //else
        //{
        //    using (esTransactionScope tr = new esTransactionScope())
        //    {
        //        foreach (FinReceipt temp in finReceiptCollection)
        //        {
        //            temp.IsDeleted = true;
        //        }
        //        finReceiptCollection.Save();

        //        FinUploadLog finUploadLog = new FinUploadLog();
        //        if (finUploadLog.LoadByPrimaryKey(Id))
        //        {
        //            finUploadLog.IsDeleted = true;
        //            finUploadLog.Save();

        //        }
        //        tr.Complete();
        //        ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showMsg('此Excel上传的凭证有已经成功批量删除！')", true);
        //    }
        //}

    }
    #endregion
}